package com.tianqu.tms.core.webservice.weichat.api.impl;

import java.rmi.RemoteException;

import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.enums.ApiLogFuncType;
import com.tianqu.tms.core.util.enums.WeChatResultCode;
import com.tianqu.tms.core.webservice.weichat.api.CarManager;
import com.tianqu.tms.core.webservice.weichat.api.UserManager;

/**
 * @author Huang Wei
 *0
 */
public class UserManagerImpl  extends AbsoluteManager implements UserManager{
	
	final static UserManagerImpl INSTANCE = new UserManagerImpl();
	
	private UserManagerImpl(){}
	
	public static UserManagerImpl getInstance() {
		return INSTANCE;
	}
	@Override
	public void memberAdd(String memberId, String realName, String shortName,String errorMessage,String marketNo) {
		int r = WeChatResultCode.SUCCESS.getValue();
		try {
			StringBuffer log = new StringBuffer();
			log.append("UserManagerImpl.memberAdd execute,");
			log.append(" memberId :"+memberId);
			log.append(" realName :"+realName);
			log.append(" shortName :"+shortName);
			log.append(" marketNo :"+marketNo);
			LOGGER.info(log);
			//
			r = getTransferService().memberAdd(StringUtil.filterNull(memberId), 
					                           StringUtil.filterNull(realName), 
					                           StringUtil.filterNull(shortName), 
					                           StringUtil.filterNull(marketNo));
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				sendMobileMessage(errorMessage+",失败原因为"+WeChatResultCode.getResultCode(r).getMessage()+".");
			} 
			
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1004.getValue();
			sendMobileMessage(errorMessage+",失败原因为服务请求超时.");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1002.getValue();
			sendMobileMessage(errorMessage+",失败原因为系统异常.");
		}
		
		//api log
		StringBuffer apiLog = new StringBuffer();
		apiLog.append("memberId_"+memberId).append("_");
		apiLog.append("realName_"+realName).append("_");
		apiLog.append("shortName"+shortName);
		if(r!=WeChatResultCode.SUCCESS.getValue()) saveApiLog(ApiLogFuncType.MEMBER,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
	}

	@Override
	public void attnAdd(String attnId, String memberId, String attnName, String accessCardId, String accessCardNo,String errorMessage) {
		int r = WeChatResultCode.SUCCESS.getValue();
		try {
			StringBuffer log = new StringBuffer();
			log.append("UserManagerImpl.attnAdd execute,");
			log.append(" attnId :"+attnId);
			log.append(" memberId :"+memberId);
			log.append(" attnName :"+attnName);
			log.append(" accessCardId :"+accessCardId);
			log.append(" accessCardNo :"+accessCardNo);
			LOGGER.info(log);
			//
			r = getUserService().attnAdd(StringUtil.filterNull(attnId), 
					                           StringUtil.filterNull(memberId), 
					                           StringUtil.filterNull(attnName), 
					                           StringUtil.filterNull(accessCardId), 
					                           StringUtil.filterNull(accessCardNo));
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				sendMobileMessage(errorMessage+",失败原因为"+WeChatResultCode.getResultCode(r).getMessage()+".");
			} 
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1004.getValue();
			sendMobileMessage(errorMessage+",失败原因为服务请求超时.");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1002.getValue();
			sendMobileMessage(errorMessage+",失败原因为系统异常.");
		}
		
		//api log
		StringBuffer apiLog = new StringBuffer();
		apiLog.append("attnId_"+attnId).append("_");
		apiLog.append("memberId_"+memberId).append("_");
		apiLog.append("attnName_"+attnName).append("_");
		apiLog.append("accessCardId_"+accessCardId).append("_");
		apiLog.append("accessCardNo_"+accessCardNo);
		if(r!=WeChatResultCode.SUCCESS.getValue()) saveApiLog(ApiLogFuncType.MEMBER,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
	}

	@Override
	public void memberUpdateCarPort(String memberId, int carPort,int recommend,String errorMessage) {
		int r = WeChatResultCode.SUCCESS.getValue();
		try {
			StringBuffer log = new StringBuffer();
			log.append("UserManagerImpl.memberUpdateCarPort execute,");
			log.append(" memberId :"+memberId);
			log.append(" carPort :"+carPort);
			log.append(" recommend :"+recommend);
			LOGGER.info(log);
			//
			r = getTransferService().memberUpdateCarPort(StringUtil.filterNull(memberId),carPort);
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				sendMobileMessage(errorMessage+",失败原因为"+WeChatResultCode.getResultCode(r).getMessage()+".");
			} 
			//api log
			StringBuffer apiLog = new StringBuffer();
			apiLog.append("memberId_"+memberId).append("_");
			apiLog.append("carPort_"+carPort);
			if(r!=WeChatResultCode.SUCCESS.getValue()) saveApiLog(ApiLogFuncType.MEMBER,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1004.getValue();
			sendMobileMessage(errorMessage+",失败原因为服务请求超时.");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1002.getValue();
			sendMobileMessage(errorMessage+",失败原因为系统异常.");
		}
	}

	@Override
	public void memberUpdateAccessCardInfo(String memberId, String accessCardId, String accessCardNo,String errorMessage) {
		int r = WeChatResultCode.SUCCESS.getValue();
		try {
			StringBuffer log = new StringBuffer();
			log.append("UserManagerImpl.memberUpdateAccessCardInfo execute,");
			log.append(" memberId :"+memberId);
			log.append(" accessCardId :"+accessCardId);
			log.append(" accessCardNo :"+accessCardNo);
			LOGGER.info(log);
			//
			r = getUserService().memberUpdateAccessCardInfo(StringUtil.filterNull(memberId),
					                                            StringUtil.filterNull(accessCardId),
					                                            StringUtil.filterNull(accessCardNo));
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				sendMobileMessage(errorMessage+",失败原因为"+WeChatResultCode.getResultCode(r).getMessage()+".");
			} 
			//api log
			StringBuffer apiLog = new StringBuffer();
			apiLog.append("memberId_"+memberId).append("_");
			apiLog.append("accessCardId_"+accessCardId).append("_");
			apiLog.append("accessCardNo_"+accessCardNo);
			if(r!=WeChatResultCode.SUCCESS.getValue()) saveApiLog(ApiLogFuncType.MEMBER,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1004.getValue();
			sendMobileMessage(errorMessage+",失败原因为服务请求超时.");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1002.getValue();
			sendMobileMessage(errorMessage+",失败原因为系统异常.");
		}	
	  }

	@Override
	public void attnUpdateAccessCardInfo(String attnId, String accessCardId, String accessCardNo,String errorMessage) {
		int r = WeChatResultCode.SUCCESS.getValue();
		try {
			StringBuffer log = new StringBuffer();
			log.append("UserManagerImpl.attnUpdateAccessCardInfo execute,");
			log.append(" attnId :"+attnId);
			log.append(" accessCardId :"+accessCardId);
			log.append(" accessCardNo :"+accessCardNo);
			LOGGER.info(log);
			//
			r = getUserService().attnUpdateAccessCardInfo(StringUtil.filterNull(attnId),
					                                            StringUtil.filterNull(accessCardId),
					                                            StringUtil.filterNull(accessCardNo));
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				sendMobileMessage(errorMessage+",失败原因为"+WeChatResultCode.getResultCode(r).getMessage()+".");
			} 
			//api log
			StringBuffer apiLog = new StringBuffer();
			apiLog.append("attnId_"+attnId).append("_");
			apiLog.append("accessCardId_"+accessCardId).append("_");
			apiLog.append("accessCardNo_"+accessCardNo).append("_");
			if(r!=WeChatResultCode.SUCCESS.getValue()) saveApiLog(ApiLogFuncType.MEMBER,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1004.getValue();
			sendMobileMessage(errorMessage+",失败原因为服务请求超时.");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1002.getValue();
			sendMobileMessage(errorMessage+",失败原因为系统异常.");
		}	
	}
	 
	/* 
	 * memberId      会员ID（商户会员ID或经办人会员ID）
	 * memberType    会员类别（1：商户会员、2：经办人会员）
	 * integral      积分(增加或减少)   
	 * chargeType    操作类型，2：交易票缴费获得积分， 4：交易票退费扣除积分       
	 * serial        车辆流水号
	 * licensePlate  车牌号码
	 * transferPrice 收费金额（交易票实收金额）
	 */
	@Override
	public void integralRegister(String memberId, int memberType, String serial, String licensePlate, int chargeType, int transferPrice, int integral,String errorMessage) {
		int r = WeChatResultCode.SUCCESS.getValue();
		try {
			StringBuffer log = new StringBuffer();
			log.append("UserManagerImpl.integralRegister execute,");
			log.append(" memberId :"+memberId);
			log.append(" memberType :"+memberType);
			log.append(" serial :"+serial);
			log.append(" licensePlate :"+licensePlate);
			log.append(" chargeType :"+chargeType);
			log.append(" transferPrice :"+transferPrice);
			log.append(" integral :"+integral);
			LOGGER.info(log);
			//
			r = getUserService().integralRegister(memberId, memberType, serial, licensePlate, chargeType, transferPrice, integral);
					                                             
			if(WeChatResultCode.SUCCESS != WeChatResultCode.getResultCode(r)){
				LOGGER.error(log+" result:"+r);
				sendMobileMessage(errorMessage+",失败原因为"+WeChatResultCode.getResultCode(r).getMessage()+".");
			} 
			//api log
			StringBuffer apiLog = new StringBuffer();
			apiLog.append("memberId_"+memberId).append("_");
			apiLog.append("accessCardId_"+memberType).append("_");
			apiLog.append("accessCardNo_"+serial).append("_");
			apiLog.append("licensePlate_"+licensePlate).append("_");
			apiLog.append("chargeType_"+chargeType).append("_");
			apiLog.append("transferPrice_"+transferPrice).append("_");
			apiLog.append("integral_"+integral).append("_");
			if(r!=WeChatResultCode.SUCCESS.getValue()) saveApiLog(ApiLogFuncType.INTEGRAL,apiLog.toString(),WeChatResultCode.getResultCode(r).getMessage());
		}catch (RemoteException e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1004.getValue();
			sendMobileMessage(errorMessage+",失败原因为服务请求超时.");
		}catch (Exception e) {
			LOGGER.error("send message error.",e);
			r = WeChatResultCode.ERROR_1002.getValue();
			sendMobileMessage(errorMessage+",失败原因为系统异常.");
		}
	}
	
	public static UserManagerImpl create() {
		return INSTANCE;
	}
}
